<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="计划名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="coursePlanId">
              <!--              <a-input v-model="model.coursePlanId" placeholder="请输入计划id"  ></a-input>-->
              <a-select v-model="model.coursePlanId" placeholder="请选择计划"
                        :getPopupContainer="(target) => target.parentNode">
                <a-select-option v-for="item in coursePlanList" :key="item.value">{{ item.label }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="关联对象类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="relObjTtype">
              <j-dict-select-tag @change="handleChanegObjType" type="list" v-model="model.relObjTtype"
                                 dictCode="course_target_obj_ttype" placeholder="请选择关联对象类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="目标对象" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="relObjId">
              <a-select v-model="model.relObjId" placeholder="请输入目标对象"
                        :getPopupContainer="(target) => target.parentNode">
                <a-select-option v-for="item in objectList" :key="item.value">{{ item.label }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <!--          <a-col :span="24">-->
          <!--            <a-form-model-item label="关联对象类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="relObjTtype">-->
          <!--              <a-input-number v-model="model.relObjTtype" placeholder="请输入关联对象类型" style="width: 100%" />-->
          <!--            </a-form-model-item>-->
          <!--          </a-col>-->
          <a-col :span="24">
            <a-form-model-item label="计划执行状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="courPlanExecStatus">
              <a-input-number v-model="model.courPlanExecStatus" placeholder="请输入计划执行状态" style="width: 100%" />
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="是否有效" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="izOpen">-->
<!--              <a-input-number v-model="model.izOpen" placeholder="请输入是否有效" style="width: 100%" />-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
          <a-col :span="24">
            <a-form-model-item label="创建时间(计划下达时间）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="createTime">
              <j-date placeholder="请选择创建时间(计划下达时间）" v-model="model.createTime" style="width: 100%" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import {
    getCoursePlanDataList,
    getSubjectList,
    getClassroomDataList,
    getAllClass,
    getClassStudentQueryAll,
    getTeacherList
  } from '@api/commonAPI'
  export default {
    name: 'RelCoursePlanAppForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           courPlanExecStatus: [
              { required: true, message: '请输入计划执行状态（1待执行，2已执行，3再次执行，4废除不执行）!'},
           ],
           izOpen: [
              { required: true, message: '请输入是否有效（0否无效，1是有效）!'},
           ],
           izDelete: [
              { required: true, message: '请输入是否删除（0否，1是已删除）!'},
           ],
           createTime: [
              { required: true, message: '请输入创建时间(计划下达时间）!'},
           ],
        },
        url: {
          add: "/relcourseplanapp/relCoursePlanApp/add",
          edit: "/relcourseplanapp/relCoursePlanApp/edit",
          queryById: "/relcourseplanapp/relCoursePlanApp/queryById"
        },
        objectList: [],
        coursePlanList: [],
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      this.initCoursePlanData();
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      async initCoursePlanData() {
        let res = await getCoursePlanDataList()
        if (res.success) {
          this.coursePlanList = []
          res.result.forEach(item => {
            this.coursePlanList.push({
              label: item.coursePlanName,
              value: item.id
            })
          })
        }
      },
      handleChanegObjType() {
        let type = this.model.relObjTtype
        this.objectList = []
        this.model.relObjId = ''
        switch (type) {
          case '1':
            this.getClassesList()
            break
          case '2' :
            this.getClassroomList()
            break
          case '3':
            this.getTeacherList()
            break
          case '4':
            this.getSubjectList()
            break
          case '5':
            this.getStudentList()
            break
          default :
            this.$message.error('获取初始化数组出错')
        }
      },
      async getClassesList() {
        let res = await getAllClass()
        if (res.success) {
          res.result.forEach(item => {
            this.objectList.push({
              label: item.className,
              value: item.id
            })
          })
        } else {
          this.$message.error('获取初始化数组出错')
        }
      },
      async getClassroomList() {
        let res = await getClassroomDataList()
        if (res.success) {
          res.result.forEach(item => {
            this.objectList.push({
              label: item.classroomName,
              value: item.id
            })
          })
        } else {
          this.$message.error('获取初始化数组出错')
        }
      },
      async getTeacherList() {
        let res = await getTeacherList()
        if (res.success) {
          res.result.forEach(item => {
            this.objectList.push({
              label: item.teacherName,
              value: item.teacherId
            })
          })
        } else {
          this.$message.error('获取初始化数组出错')
        }
      },
      async getSubjectList() {
        let res = await getSubjectList()
        if (res.success) {
          res.result.forEach(item => {
            this.objectList.push({
              label: item.subjectName,
              value: item.subjectId
            })
          })
        } else {
          this.$message.error('获取初始化数组出错')
        }
      },
      async getStudentList() {
        let res = await getClassStudentQueryAll()
        if (res.success) {
          res.result.forEach(item => {
            this.objectList.push({
              label: item.studentName,
              value: item.id
            })
          })
        } else {
          this.$message.error('获取初始化数组出错')
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>